home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10856 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Variant Records in C ... Is there a way ?
  5. Date: Wed, 20 Mar 1996 15:24:08 +0200
  6. Organization: Carelcomp Products
  7. Message-ID: <315006F8.639@cmt.lpr.mail.carel.fi>
  8. References: <Pine.OSF.3.91.960319170252.9783B-100000@alfa.ist.utl.pt>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Nuno Silva wrote:
  16. >         I would like to know if there is any  way of implementing in C a
  17. > structure that if a determined argument (or info or depending on the
  18. > value of a particular field of a structure) could ignore or acknoledge a
  19. > certain field of the structure ...
  20. > [snip]
  21.  
  22. How about something like:
  23.  
  24.     struct {
  25.         int    flag;
  26.         union {
  27.             char    text[80];
  28.             int    num;
  29.             double    fpoint;
  30.         } data;
  31.     } test;
  32.  
  33. And then, in the code:
  34.  
  35.     switch (test.flag) {
  36.         case FLAG_TEXT:
  37.             /* use test.data.text */
  38.             break;
  39.         case FLAG_NUM:
  40.             /* use test.data.num */
  41.             etc.
  42.     }
  43.  
  44. Later,
  45.  AriL
  46. -- 
  47. All my opinions are mine and mine alone.
  48.